{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.cnblogs.com/lixiang1013/articles/9031296.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*** print_tb:\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\n",
      "    lumberjack()\n",
      "*** print_exception:\n",
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\n",
      "    lumberjack()\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\n",
      "    bright_side_of_death()\n",
      "IndexError: tuple index out of range\n",
      "*** print_exc:\n",
      "*** format_exc, first and last line:\n",
      "Traceback (most recent call last):\n",
      "IndexError: tuple index out of range\n",
      "*** format_exception:\n",
      "['Traceback (most recent call last):\\n', '  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\\n    lumberjack()\\n', '  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\\n    bright_side_of_death()\\n', '  File \"<ipython-input-6-c4d53df3d407>\", line 7, in bright_side_of_death\\n    return tuple()[0]\\n', 'IndexError: tuple index out of range\\n']\n",
      "*** extract_tb:\n",
      "[<FrameSummary file <ipython-input-6-c4d53df3d407>, line 10 in <module>>, <FrameSummary file <ipython-input-6-c4d53df3d407>, line 4 in lumberjack>, <FrameSummary file <ipython-input-6-c4d53df3d407>, line 7 in bright_side_of_death>]\n",
      "*** format_tb:\n",
      "['  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\\n    lumberjack()\\n', '  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\\n    bright_side_of_death()\\n', '  File \"<ipython-input-6-c4d53df3d407>\", line 7, in bright_side_of_death\\n    return tuple()[0]\\n']\n",
      "*** tb_lineno: 10\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\n",
      "    lumberjack()\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\n",
      "    bright_side_of_death()\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 7, in bright_side_of_death\n",
      "    return tuple()[0]\n",
      "IndexError: tuple index out of range\n"
     ]
    }
   ],
   "source": [
    "import sys, traceback\n",
    "\n",
    "def lumberjack():\n",
    "    bright_side_of_death()\n",
    "\n",
    "def bright_side_of_death():\n",
    "    return tuple()[0]\n",
    "\n",
    "try:\n",
    "    lumberjack()\n",
    "except IndexError:\n",
    "    a = traceback.format_exc()\n",
    "    exc_type, exc_value, exc_traceback = sys.exc_info()\n",
    "    print(\"*** print_tb:\")\n",
    "    traceback.print_tb(exc_traceback, limit=1, file=sys.stdout)\n",
    "\n",
    "    print(\"*** print_exception:\")\n",
    "    traceback.print_exception(exc_type, exc_value, exc_traceback,\n",
    "                              limit=2, file=sys.stdout)\n",
    "    print(\"*** print_exc:\")\n",
    "    traceback.print_exc()\n",
    "    print(\"*** format_exc, first and last line:\")\n",
    "    formatted_lines = traceback.format_exc().splitlines()\n",
    "    \n",
    "    print(formatted_lines[0])\n",
    "    print(formatted_lines[-1])\n",
    "    print(\"*** format_exception:\")\n",
    "    print(repr(traceback.format_exception(exc_type, exc_value,\n",
    "                                          exc_traceback)))\n",
    "    print(\"*** extract_tb:\")\n",
    "    print(repr(traceback.extract_tb(exc_traceback)))\n",
    "    print(\"*** format_tb:\")\n",
    "    print(repr(traceback.format_tb(exc_traceback)))\n",
    "    print(\"*** tb_lineno:\", exc_traceback.tb_lineno)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Traceback (most recent call last):\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\n",
      "    lumberjack()\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\n",
      "    bright_side_of_death()\n",
      "  File \"<ipython-input-6-c4d53df3d407>\", line 7, in bright_side_of_death\n",
      "    return tuple()[0]\n",
      "IndexError: tuple index out of range\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Traceback (most recent call last):\\n  File \"<ipython-input-6-c4d53df3d407>\", line 10, in <module>\\n    lumberjack()\\n  File \"<ipython-input-6-c4d53df3d407>\", line 4, in lumberjack\\n    bright_side_of_death()\\n  File \"<ipython-input-6-c4d53df3d407>\", line 7, in bright_side_of_death\\n    return tuple()[0]\\nIndexError: tuple index out of range\\n'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
